<?php
namespace app\api\controller;

/**
 * 小程序推送接收
 */
class ZhReceive extends Common {

    //仅供测试使用
    public function test(){
        return false;
        //测试验证不通过的情况
        $postStr = '{"ToUserName":"gh_3e28ab1b830b","FromUserName":"oO_Sz62UxyHrA8RXQJZdQQcPyaC0","CreateTime":1716519460,"MsgType":"event","Event":"wxa_media_check","appid":"wxad55234424836d7d","trace_id":"66500222-511fbdf2-02d74d8b","version":2,"detail":[{"strategy":"content_model","errcode":0,"suggest":"pass","label":100,"prob":90}],"errcode":0,"errmsg":"ok","result":{"suggest":"pass","label":1100}}';
        $notifyData = json_decode($postStr);
        print_r($notifyData);
        if ($notifyData->MsgType == 'event') {
            //图片检测回调
            if ($notifyData->Event == 'wxa_media_check') {
                $this->dealMediaCheck($notifyData);
            }
        }
    }

    public function index(){
        //消程序通知平台验证使用
        if (isset($_GET['echostr'])) {
            $this->valid();
        }
        //回调消息，通过GET方式传递签名信息，JSON方式传递body数据
        if($this->checkSignature() === false){
            return false;
        }
        $postStr = file_get_contents('php://input');
        //file_put_contents('wechat_notify.txt', $postStr. json_encode($_GET) . json_encode($_POST) ."\n", FILE_APPEND);
        $notifyData = json_decode($postStr);
        if ($notifyData->MsgType == 'event') {
            //图片检测回调
            if ($notifyData->Event == 'wxa_media_check') {
                $this->dealMediaCheck($notifyData);
            }
        }
        $str = '{"ErrCode":0,"ErrMsg":"success"}';
        return $str;
    }

    private function dealMediaCheck($notifyData) {
        //更新日志中的处理结果
        $data = ['suggest' => $notifyData->result->suggest, 'label' => $notifyData->result->label, 'updatetime' => time()];
        $log = db('zh_wechat_media_log')->where('trace_id', $notifyData->trace_id)->find();
        db('zh_wechat_media_log')->where('id', $log['id'])->update($data);
        //如果状态不是100，将资料更新为待审核
        if($notifyData->result->label != 100){
            if($log['authentication_id'] > 0){
                \app\api\model\ZhAuthentication::where("authentication_id", $log['authentication_id'])->update(['status' => 0]);
            }
            if($log['matchmaker_id'] > 0){
                \app\api\model\ZhMatchmaker::where("matchmaker_id", $log['matchmaker_id'])->update(['status' => 0]);
            }
        }
    }


    //签名验证公共接口
    private function valid() {
        $echoStr = $_GET["echostr"];
        if ($this->checkSignature()) {
            echo $echoStr;
            exit;
        }
    }

    private function checkSignature() {
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];

        $token = config('my.mini_program.token');
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode($tmpArr);
        $tmpStr = sha1($tmpStr);

        if ($tmpStr == $signature) {
            return true;
        } else {
            return false;
        }
    }

}